// pages/map/map.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		data: '',
		nan: '',
		uid:''
	},
	onShow() {
		wx.getStorage({
			key: 'uid',
			success: (e) => {
				wx.request({
					url: getApp().globalData.http + "action=getAddressList&uid=" + e.data.uid,
					success: (res) => {
						console.log(res)
						if (res.data.data.length > 0) {
							this.setData({
								data: res.data.data,
								nan: 1,
								uid:e.data.uid
							})
						} else {
							this.setData({
								data: res.data.data,
								nan: 0,
								uid:e.data.uid
							})
						}

					}
				})
			}
		})

	},

	gb(e){
		console.log(e)
		wx.request({
			url:getApp().globalData.http+'action=setDefaultAddress&uid='+this.data.uid+'&id='+this.data.data[e.detail.value].ADDR_ID,
			success: (res) => {
				console.log(res)
				wx.showToast({
					title:'设置成功',
					icon:'none'
				})
			}
		})
	},
	delete1(e){
		console.log(e)
		wx.showModal({
			title:'确定要删除该地址吗？',
			success: (res) => {
				console.log(res.confirm)
				if(res.confirm==1){
					wx.request({
						url:getApp().globalData.http+'action=deleteAddress&uid='+this.data.uid+'&id='+this.data.data[e.currentTarget.dataset.id].ADDR_ID,
						success: (c) => {
							console.log(c)
							this.setData({
								data:c.data.data
							})
							wx.showToast({
								title:'删除成功',
								icon:'none'
							})
							let pages = getCurrentPages(); // 当前页的数据，可以输出来看看有什么东西
							let prevPage = pages[pages.length - 2];
							console.log(prevPage.route)
								prevPage.setData({
									add: '',
									pd:0,
								})
							if(this.data.data.length<1){
								this.setData({
									nan:0
								})
							}
						}
					})
				}
			}
		})
		
	},
	map(e){
		console.log(e)
		wx.request({
			url:getApp().globalData.http+'action=showAddress&ID='+this.data.data[e.currentTarget.dataset.id].ADDR_ID,
			success: (res) => {
				console.log(res)
				let pages = getCurrentPages(); // 当前页的数据，可以输出来看看有什么东西
				let prevPage = pages[pages.length - 2];
				prevPage.setData({
					add: res.data.data,
					pd:0,
				})
				wx.navigateBack({
					delta: 1
				})
			}
		})
		
	}
})
